package com.tanhua.manager;

import com.tanhua.domain.Admin;
import com.tanhua.domain.AdminVo;
import com.tanhua.interceptor.AdminHolder;
import com.tanhua.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping(value = "/system")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping(value ="/users/verification",name = "生成验证码")
    public ResponseEntity verification(String uuid, HttpServletResponse response) throws IOException {
        //调用service生成验证码
        return adminService.verification(uuid,response);
    }


    @PostMapping(value ="/users/login",name = "登录")
    public ResponseEntity login(@RequestBody Map<String,String> map) throws IOException {

        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        //调用service生成验证码
        return adminService.login(username,password,verificationCode,uuid);
    }


    @PostMapping(value ="/users/profile",name = "携带用户数据到首页")
    public ResponseEntity profile() throws IOException {

        AdminVo adminVo = new AdminVo();
        Admin admin = AdminHolder.getAdmin();
        BeanUtils.copyProperties(admin,adminVo);
        return ResponseEntity.ok(adminVo);
    }

    @PostMapping(value ="/users/logout",name = "用户退出")
    public ResponseEntity logout() throws IOException {

        //根据对应的token将redis中的对象给删除了
        return adminService.logout();



    }



}
